﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace MailService
{
	partial class MailService : ServiceBase
	{
		System.Timers.Timer timer1;//计时器
		public MailService()
		{
			InitializeComponent();
		}

		protected override void OnStart(string[] args)
		{
			EventLog.WriteEntry("邮件发送服务以启动123");
			timer1 = new System.Timers.Timer();
			//检查软件设置是否加载成功
            //if (HttpRuntime.Cache["SleepTime"] == null)
            //{
            //    MailHelper.GetSettings();
            //}
			//设置计时器事件间隔执行时间
			timer1.Interval = Convert.ToInt32(60000);
			timer1.Elapsed += timer1_Elapsed;
			timer1.Enabled = true;
		}

		void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
		{
			//执行SQL语句或其他操作
			try
			{
                File.AppendAllText("c:/1.txt", "开始发送\r\n");
				MailHelper.StartSend();
			}
			catch (Exception ex)
			{
				EventLog.WriteEntry(ex.Message);
			}

		}

		protected override void OnStop()
		{
			this.timer1.Enabled = false;
			EventLog.WriteEntry("邮件发送服务以停止");
		}
	}
}
